package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.*;
import net.qhzw.vip.qiyu.entity.DtsBatteryManage;
import net.qhzw.vip.qiyu.handler.MyException;
import net.qhzw.vip.qiyu.model.DtsBatteryManageVo;
import net.qhzw.vip.qiyu.model.DtsCellExcel;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.DtsBatteryHistoryService;
import net.qhzw.vip.qiyu.service.DtsBatteryManageService;
import net.qhzw.vip.qiyu.service.DtsBatteryManageService;
import net.qhzw.vip.qiyu.service.DtsBatteryStatusService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("dts/batteryrManage")
@Api(tags = "电池维护管理")
public class DtsBatteryManageController {


    @Autowired
    private DtsBatteryManageService dtsBatteryManageService;
    @Autowired
    private DtsBatteryHistoryService dtsBatteryHistoryService;
    @Autowired
    private DtsBatteryStatusService dtsBatteryStatusService;



    @ApiOperation(value = "分页查询电池维护")
    @GetMapping
    public JsonResult findByPage(Page<DtsBatteryManage> page, String electricCabinetCode, Long batteryStatus,String electricCabinetName,
                                 String batteryCode) {
        //Long shopId = SecurityUtils.getLoginUser().getShopId();
        Map<String, Object> result = dtsBatteryManageService.findByPage(page, electricCabinetCode,batteryStatus,electricCabinetName,batteryCode);
        return JsonResult.ok(result);
    }
    //@ApiOperation(value = "退车费用分页列表查询")
    //@GetMapping
    //public JsonResult findByPage(Page<DtsCarRetreat> page, String idCard, String mobile, String realName, Long siteId) {
    //    Long shopId = SecurityUtils.getLoginUser().getShopId();
    //    Map<String, Object> result = dtsCarRetreatService.findByPage(page, idCard, mobile, realName, siteId, shopId);
    //    return JsonResult.ok(result);
    //}

    @ApiOperation(value = "根据ID查询电池维护")
    @GetMapping("{id}")
    public JsonResult<DtsBatteryManage> findDtsBatteryManage(@PathVariable Long id) {
        DtsBatteryManage dtsBatteryManagePage = dtsBatteryManageService.getById(id);
        //DtsBatteryHistory dtsBatteryHistory = dtsBatteryHistoryService.getById(id);
        return JsonResult.ok(dtsBatteryManagePage);
    }

    /**
     * 查询最新状态
     * @param batteryCode
     * @return
     */
    @ApiOperation(value = "根据ID查询电池最新状态")
    @GetMapping("/status/details/{batteryCode}")
    public JsonResult findDtsBatteryStatus(@PathVariable String batteryCode) {
        DtsBatteryManageVo dtsBatteryManageVo = dtsBatteryHistoryService.selectHisAnddts(batteryCode);
        if (dtsBatteryManageVo == null){
            DtsBatteryManageVo dtsBatteryManageVo1 = new DtsBatteryManageVo();
            dtsBatteryManageVo1.setStatusValue("暂无状态");

            return JsonResult.ok(dtsBatteryManageVo1);
        }
        return JsonResult.ok(dtsBatteryManageVo);

    }
    @ApiOperation(value = "根据ID查询电池维护")
    @GetMapping("/details/{id}")
    public JsonResult findDtsBatteryDetailManage(@PathVariable Long id) {
        //DtsBatteryManage dtsBatteryManagePage = dtsBatteryManageService.getById(id);
        List<DtsBatteryHistory> list = dtsBatteryHistoryService.selectListHisDetail(id);
        List<DtsBatteryStatus> list1 = dtsBatteryStatusService.list(new LambdaQueryWrapper<DtsBatteryStatus>()
                .eq(DtsBatteryStatus::getIsDelete, 0));
        for (DtsBatteryHistory dtsBatteryHistory : list) {
            DtsBatteryStatus dtsBatteryStatus = dtsBatteryStatusService.getById(dtsBatteryHistory.getHistoryState());
            dtsBatteryHistory.setValue(dtsBatteryStatus.getStatusValue());
            //for (DtsBatteryStatus dtsBatteryStatus : list1) {
            //    if (dtsBatteryHistory.getHistoryState() == dtsBatteryStatus.getId()){
            //        dtsBatteryHistory.setValue(dtsBatteryStatus.getStatusValue());
            //    }
            //}
        }
        
        return JsonResult.ok(list);
    }


    @ApiOperation(value = "添加电池信息")
    @PostMapping
    public JsonResult addDtsBatteryManage(@RequestBody DtsBatteryManage dtsBatteryManage) {

        boolean save = dtsBatteryManageService.addDtsBatteryManage(dtsBatteryManage);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "修改电池维护")
    @PutMapping
    public JsonResult updateDtsBatteryManage(@RequestBody DtsBatteryManage dtsBatteryManage) {
        boolean save = dtsBatteryManageService.updateDtsBatteryManage(dtsBatteryManage);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "设置电池维修状态")
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody DtsBatteryManage dtsBatteryManage) {
        boolean save = dtsBatteryManageService.updateDtsBatteryManage(dtsBatteryManage);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "删除电池维护")
    @DeleteMapping("{id}")
    public JsonResult<DtsBatteryManage> findDtsBatteryManage(@PathVariable String id) {
        List<String> list = Arrays.asList(id.split(","));
        boolean remove = dtsBatteryManageService.removeByIds(list);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }
    @ApiOperation(value = "表格导入电池信息值")
    @PostMapping("importBattery")
    public JsonResult importBattery(@RequestParam("file") MultipartFile file) {
        //Long shopId = SecurityUtils.getLoginUser().getShopId();
        //Long userId = SecurityUtils.getLoginUser().getUserId();
        //if (shopId == 1) {
        //    throw new ApiException("您不能充值，请登录商户账号进行操作！");
        //}
        dtsBatteryManageService.importBattery(file);
        return JsonResult.ok();
    }

    @ApiOperation(value = "小程序添加电池信息")
    @PostMapping("/addDtsBatteryList")
    public JsonResult addDtsBatteryManages(@RequestBody DtsBatteryManage dtsBatteryManage) {

        boolean save = dtsBatteryManageService.saveBatterys(dtsBatteryManage);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "小程序添加电池信息录入")
    @PostMapping("/addDtsBatteryListSuper")
    public JsonResult addDtsBatteryListSuper(@RequestBody DtsBatteryManage dtsBatteryManage) {

        boolean save = dtsBatteryManageService.saveBatteryList(dtsBatteryManage);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    //@ApiOperation(value = "表格导入电池信息值")
    //@PostMapping("importBar")
    //public JsonResult importRecharge(@RequestParam("file") MultipartFile file) {
    //    //Long shopId = SecurityUtils.getLoginUser().getShopId();
    //    //if (shopId == 1) {
    //    //    throw new ApiException("您不能充值，请登录商户账号进行操作！");
    //    //}
    //    dtsBatteryManageService.importRecharge(file);
    //    return JsonResult.ok();
    //}


    /**
     * 导出电池使用信息
     */
    @ApiOperation(value = "导出电池使用信息")
    @GetMapping("exportBattery")
    public void exportBattery( HttpServletResponse response,String electricCabinetCode, Long batteryStatus,String electricCabinetName,
                               String batteryCode) throws IOException {
        dtsBatteryManageService.exportBattery( response,electricCabinetCode,  batteryStatus, electricCabinetName, batteryCode);
    }

}
